package com.lb.trac.controller.common.loaders;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lb.trac.pojo.Progetti;
import com.lb.trac.pojo.Utenti;
import com.lb.trac.pojo.comparator.OrderBy;
import com.lb.trac.pojo.comparator.ProgettiOrderByDescrizione;
import com.lb.trac.service.SearchService;
import com.lb.trac.util.ControllerUtils;

public class LoadProgetti implements HttpObjectsLoader {

	private SearchService searchService;

	public void load(HttpServletRequest request, HttpServletResponse response) {
		if(request.getSession(false)==null){
			try {
				request.getRequestDispatcher("login.do").forward(request, response);
			} catch (ServletException e) {
			} catch (IOException e) {
			}
			return;
		}
		Progetti[] progettis =  getSearchService()
					.find(new Progetti[0], Progetti.class);
			request.setAttribute("progetti", progettis);
		
		Utenti uv = ControllerUtils.getUtenteLoggato(request);
		progettis =(Progetti[]) uv.getProgettis().toArray(new Progetti[0]);
			Arrays.sort(progettis, new ProgettiOrderByDescrizione(OrderBy.ASC));
			request.setAttribute("progettiUtente", progettis); 
		
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

}
